<script>
	import Vue from 'vue'
	import request from '@/common/request.js';
	export default {
		globalData: {
			timer:null,
			nowTime: '', //日期
			nowTime2: '', //时间
			addEntity:{
				JobId:0,
				Longitude:'',
				Latitude:'',
				LoginType:1
			},
		},
		onLaunch: function() {
			this.checkIsArea()
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight + 4;
					// #endif		

					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			});
		},
		onShow: function() {
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods:{
			checkIsArea(){
				let that=this
				uni.getStorage({
					key: 'userdata',
					success: function (UserDataRes) {
						let isArea=UserDataRes.data.isArea
						if(isArea===1){
							//console.log("需要获取实时位置")
							that.getIntervalPosition();
						}else{
							//console.log("不需要获取实时位置")
						}
					}
				})
			},
			clearSetTime(){
				let that=this
				clearTimeout(that.timer);  
			},
			//定时上传位置信息
			getIntervalPosition(){
				let that=this
				clearTimeout(that.timer);
				that.timer=setInterval(()=>{
					console.log("getIntervalPosition")
					that.location();
				},60000)
			},
			// 这个函数 在 onLoad内触发 或 点击触发
			location(){
			    const that = this;
				try
				{
					that.getWxLocation();
			    }
				catch(error)
				{
			        Model({
						title: '温馨提示',
						tip: '获取权限失败，需要获取您的地理位置才能为您提供更好的服务！是否授权获取地理位置？',
						showCancel: true,
						confirmText: '前往设置',
						cancelText: '取消',
			        sureCall(){
			            that.toSetting()
			        },
						cancelCall() {}
			        })
			        return;
			    }
			},
			// 获取位置信息
			getWxLocation() {
				let that=this
				// wx.showLoading({
				//   title: '定位中...',
				//   mask: true,
				// })
			    return new Promise((resolve, reject) => {
			        const _locationChangeFn = (res) => {
			        console.log('location change', res)
				  
				    that.globalData.addEntity.Longitude= res.longitude;
				    that.globalData.addEntity.Latitude= res.latitude;
					uni.getStorage({
						key: 'JobId',
						success: function (res3) {
							console.log('33')
							that.globalData.addEntity.JobId=res3.data.id;
							let opts = {
								url: 'api/AddUserPosition',
								method: 'post'
							};
							request.httpTokenRequest(opts,that.globalData.addEntity).then(res4 => {
								that.getTime()
								if(res4.data.code == 1){
									console.log('上传位置信息success：'+that.nowTime2)
								}
								else{
									console.log('上传位置信息fail')
								}
							});
						},fail() {
						}
				  })
			      wx.hideLoading()
			      wx.offLocationChange(_locationChangeFn)
			    }
			    wx.startLocationUpdate({
			      success: (res) => {
			        wx.onLocationChange(_locationChangeFn)
			        resolve()
			      },
			      fail: (err) => {
			        reject()
			      }
			    })
			  })
			},
			
			// 调起客户端小程序设置界面
			toSetting() {
			  wx.openSetting({
			    success(res) {
			      if (res.authSetting["scope.userLocation"]) {
			        this.authorization()
			      }
			    }
			  })
			},
			getTime: function() {
				var date = new Date(),
					year = date.getFullYear(),
					month = date.getMonth() + 1,
					day = date.getDate(),
					hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
					minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
					second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
				month >= 1 && month <= 9 ? (month = "0" + month) : "";
				day >= 0 && day <= 9 ? (day = "0" + day) : "";
				var timer = year + '年' + month + '月' + day + '日';
				var timer2 = hour + ':' + minute + ':' + second;
				this.nowTime = timer;
				this.nowTime2 = timer2;
			},
		}
		
	}
</script>

<style lang="scss">
	@import "colorui/main.css";
	@import "colorui/icon.css";
	@import "uview-ui/index.scss";
</style>
